<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Presentdiamond_Controller
 *
 * @author dragon.hu
 */
defined('BASEPATH') or die('forbidden');

class Presentdiamond_Controller extends Controller {

    private $inc;
    private $activityList; //活动集合
    private $activityTypeList;
    private $tmpconnect;

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->inc = $this->config['inc'];

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        if ($area_host == '') {
        	echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
        	return;
        }
        
        $area_host = $_SESSION['area_selected_host'];
        // 得到选择的区号
        $areanum = $_SESSION ['area_selected_id'];
        
        $this->tmpconnect = getTmpDbConnect ( PLATFORM_ALL, 'all', $areanum, DBTYPE_LOG );
        
    }

    function index() {
        $this->view->display("businessdata/presentdiamond/index");
    }
    
    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        
        $sql = "SELECT COUNT(*) AS num from (SELECT role_id,server_id, SUM(produce_gold_number)as sumP from goldproduce GROUP BY role_id) as a LEFT JOIN 
		(SELECT server_id, role_id, SUM(produce_gold_number)as sumG FROM goldproduce WHERE produce_gold_number > 0 AND produce_gold_way != 57 GROUP BY role_id) as b 
		on (a.role_id = b.role_id AND a.server_id = b.server_id) LEFT JOIN (SELECT role_id,server_id,SUM(recharge_rmb) AS sumC FROM recharge GROUP BY role_id) as c 
        on (b.role_id = c.role_id AND b.server_id = c.server_id)";
        
        /*$sql = "SELECT COUNT(DISTINCT(role_id)) AS num FROM goldproduce WHERE produce_gold_number > 0 AND produce_gold_way != 57";*/
        $one = $this->tmpconnect->one($sql);
        $total = $one['num'];
        /*print_r($colkey);
         echo "<br>";
         print_r($closinfo);
         echo "<br>";
         print_r($where);
         echo "<br>";
         print_r($sortname);
         echo "<br>";
         print_r($sortorder);
         echo "<br>";
         print_r($offset);
         echo "<br>";
         print_r($rp);
         echo "<br>";
         print_r($page);
         exit();*/
        
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        if ($where == "1=1")
        {
        	$sql = "SELECT a.role_id,a.server_id,a.sumP,b.sumG, sumC from (SELECT role_id,server_id, SUM(produce_gold_number)as sumP from goldproduce GROUP BY role_id) as a LEFT JOIN
        	(SELECT server_id, role_id, SUM(produce_gold_number)as sumG FROM goldproduce WHERE produce_gold_number > 0 AND produce_gold_way != 57 GROUP BY role_id) as b
        	on (a.role_id = b.role_id AND a.server_id = b.server_id) LEFT JOIN (SELECT role_id,server_id,SUM(recharge_rmb) AS sumC FROM recharge GROUP BY role_id) as c on 
        	(b.role_id = c.role_id AND b.server_id = c.server_id) WHERE {$where} {$orderby} LIMIT {$offset}, {$rp}";
        }else
        {
        	$sql = "SELECT a.role_id,a.server_id,a.sumP,b.sumG, sumC from (SELECT role_id,server_id, SUM(produce_gold_number)as sumP from goldproduce GROUP BY role_id) as a LEFT JOIN
        	(SELECT server_id, role_id, SUM(produce_gold_number)as sumG FROM goldproduce WHERE produce_gold_number > 0 AND produce_gold_way != 57 GROUP BY role_id) as b
        	on a.role_id = b.role_id AND a.server_id = b.server_id) LEFT JOIN (SELECT role_id,server_id,SUM(recharge_rmb) AS sumC FROM recharge GROUP BY role_id) as c on 
        	(b.role_id = c.role_id AND b.server_id = c.server_id) WHERE a.".$where." ".$orderby. " LIMIT ".$offset.",".$rp;
        }
        
        /*$sql = "SELECT server_id, role_id, SUM(produce_gold_number) AS sumG FROM goldproduce WHERE produce_gold_number > 0 AND 
        		produce_gold_way != 57 AND {$where} {$orderby} GROUP BY role_id LIMIT {$offset}, {$rp}";*/
        $this->tmpconnect->query($sql);
		$i = 1;
        while ($n = $this->tmpconnect->get_array())
        {
        	$value['id'] = $i;
        	$i++;
        	$value['server_id'] = $n['server_id'];
        	$value['role_id'] = $n['role_id'];
        	$value['sumP'] = $n['sumP'];
        	$value['sumG'] = $n['sumG'];
        	$value['sumC'] = $n['sumC'];
        	$rows[] = array('id' => $value['id'], 'cell' => $value);
        }

        //print_r($rows);
        //exit();
        //$total = count($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );

        echo json_encode($result);
    }

}

?>
